/// Author: Samle
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

/// 页面参数使用状态管理进行缓存
class SettingStore {

  /// 当前选中字典
  setBookSelected(int book) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt('books_selected', book);
  }
  getBookSelected() async {
    final prefs = await SharedPreferences.getInstance();
    final book = prefs.getInt('books_selected') ?? 0;
    return book;
  }

  /// 缓存单词列表
  setWordList(List datas) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('words_list', json.encode(datas));
  }
  getWordList() async {
    final prefs = await SharedPreferences.getInstance();
    final dataForm = json.decode(prefs.getString('words_list') ?? '[]');
    return dataForm;
  }

  /// 缓存单词列表当前下标
  setWordSelected(int index) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt('words_selected', index);
  }
  getWordSelected() async {
    final prefs = await SharedPreferences.getInstance();
    final index = prefs.getInt('words_selected') ?? 0;
    return index;
  }

}
